    var selectedLevel = 1;
    function selectedCat1Changed(pSelect)
    {
        selectedLevel = 1;
        setNameValue(pSelect);
        //setOptionsStyle(pSelect);        
        initSelect2(pSelect.form);
    }
    function selectedCat2Changed(pSelect)
    {
        selectedLevel = 2;
        setNameValue(pSelect);
        //setOptionsStyle(pSelect);        
       	initSelect3(pSelect.form);
    }
    
    function selectedCat3Changed(pSelect)
    {
        setNameValue(pSelect);
        selectedLevel = 3;
    }
    
    function initSelectedOptionVale(pSelect)
    {
        var selectedIndex = pSelect.selectedIndex;
        if(pSelect.options[selectedIndex].text.indexOf(">>") >= 0)
        {
            pSelect.options[selectedIndex].text = pSelect.options[selectedIndex].text.substring(2,pSelect.options[selectedIndex].text.length);
            pSelect.selectedIndex = selectedIndex;
        }    
    }
    
    
    function setOptionsStyle(pSelect)
    {
        var optionsSize = pSelect.options.length;
        var selectedIndex = pSelect.selectedIndex;
        if(selectedIndex == 0)return;
        for(var i=0;i<optionsSize;i++)
        {
            if(i == selectedIndex)
            {
                if(pSelect.options[i].text.indexOf(">>") < 0)
                    pSelect.options[i].text = ">>"+pSelect.options[i].text;
            }    
            else
            {
                var beginIndex = pSelect.options[i].text.indexOf(">>");
                if(beginIndex >= 0)
                {
                    pSelect.options[i].text = pSelect.options[i].text.substring(2,pSelect.options[i].text.length);
                }
            }
        }
        pSelect.selectedIndex = selectedIndex;
    }
    
    function setNameValue(pSelect)
    {
        if(pSelect.form.curpage && pSelect.selectedIndex > 0)
        {
            
            pSelect.form.name.value = pSelect.options[pSelect.selectedIndex].text;
            pSelect.form.id.value = pSelect.options[pSelect.selectedIndex].value;
        }    
    }
    
    //how to select the option from down to top
    
    
    /**
    *make one of options of the select selected
    */
    function defaultSelect(pCatIdSelect,selectedCatId)
    {
        if(pCatIdSelect == null)return;
        if(pCatIdSelect.options.length == 0)return;
        if(pCatIdSelect == selectedCatId)return;
        var option;
        var value;
        var hasDefault = false;
        for(var i=0;i<pCatIdSelect.options.length;i++)
        {
            option = pCatIdSelect.options[i];
            value = option.value;
            if(selectedCatId == value)
            {
                pCatIdSelect.selectedIndex = i;
                hasDefault = true
                break;
            }
        }
        if(!hasDefault)
        {
            if(pCatIdSelect.options.length > 1)
                pCatIdSelect.selectedIndex = 1;
            else
                pCatIdSelect.selectedIndex = 0;
        }
    }
    
    //2007-9-12,need much changes to initSelect1
    var selectedCatLevel = 0;
    var selectedLevel1CatId;    
    var selectedLevel2CatId;    
    var selectedLevel3CatId;    
    //get the path of selected cat
    function selectLevel1Cat(pForm)
    {
        var selectedCatId = pForm.catId.value;
        if(selectedCatId == null)return;
        if(selectedCatId.length == 0)return;
        var found = false;
        if(catsData != null)
        {
            if(catsData.readyState == "complete")
            {
                var catRootNode = catsData.XMLDocument.selectSingleNode("cats");
                var catLevel1Nodes = catRootNode.childNodes;
                var catLevel1Node = null;
                var text;
                var value;
                //how to find
                var catLevel2Nodes;
                var catLevel2Node = null;
                var catLevel3Nodes;
                var catLevel3Node = null;
                for(var i=0;i<catLevel1Nodes.length;i++)
                {
                    if(found)break;
                    catLevel1Node = catLevel1Nodes.item(i);
                    value = catLevel1Node.attributes.item(1).value;
                    selectedLevel1CatId = value;
                    if(selectedCatId == value)
                    {
                        //alert("1>"+value);                    
                        //l1cat = selectedCatId;
                        found = true;
                        break;
                    }
                    else
                    {
                        catLevel2Nodes = catLevel1Node.childNodes;
			for(var j=0;j<catLevel2Nodes.length;j++)
                        {
                            if(found)break;
                            catLevel2Node = catLevel2Nodes(j);
                            value = catLevel2Node.attributes.item(1).value;
                            selectedLevel2CatId = value;
                            if(selectedCatId == value)
                            {
                                //alert("2:"+value);
                                //l2cat = selectedCatId;
                                found = true;
                                break;
                            }
                            else
                            {
                                catLevel3Nodes = catLevel2Node.childNodes;
                		for(var k=0;k<catLevel3Nodes.length;k++)//level 3
                		{
                                    if(found)break;
                                    catLevel3Node = catLevel3Nodes(k);
                                    value = catLevel3Node.attributes.item(1).value;
                                    selectedLevel3CatId = value;
                                    if(selectedCatId == value)
                                    {
                                        //l3cat = selectedCatId;
                                        found = true;
                                        break;
                                    }
                                }//end for
                            }//end else
                        }//for                        
                    }//end else
                }//end for
            }//end complete
        }//
        //*/
        alert(selectedLevel1CatId+":"+selectedLevel2CatId+":"+selectedLevel3CatId);        
    }
    
    function initSelect(pForm)
    {
        selectLevel1Cat(pForm);
        initSelect1(pForm);
    }
    
    /*the question's category id
    *is used currently
    */
    function initSelect1(pForm)
    {
        if(pForm == null)
        {
            return;
        }
        var selectedCatId = pForm.catId.value;
        if(catsData != null)
        {
            if(catsData.readyState == "complete")
            {
                var cat1IdSelect = pForm.cat1Id;
                var catRootNode = catsData.XMLDocument.selectSingleNode("cats");
                //alert(catRootNode);
                var catLevel1Nodes = catRootNode.childNodes;
                //alert(catLevel1Nodes.length);
                var catLevel1Node = null;
                var catNewOption = null;
                for(var i=0;i<catLevel1Nodes.length;i++)
                {
                    catLevel1Node = catLevel1Nodes.item(i);
                    catNewOption = document.createElement("OPTION");
                    catNewOption.value = catLevel1Node.attributes.item(1).value;
                    catNewOption.text = catLevel1Node.attributes.item(2).value;
                    cat1IdSelect.add(catNewOption);
                }
                //alert("1.1");
                defaultSelect(cat1IdSelect,selectedCatId);
                //alert("1.2");
                initSelect2(pForm);
                //alert("1.3");
            }
        }    
    }
    
    
    
    function initSelect2(pForm)
    {
        var selectedCatId = pForm.catId.value;
        //alert("2.01,selectedCatId="+selectedCatId);
    	var level1CatId = pForm.cat1Id.options[pForm.cat1Id.selectedIndex].value
        var cat2IdSelect = pForm.cat2Id;
    	var catRootNode = catsData.XMLDocument.selectSingleNode("cats");
    	var catLevel1Nodes = catRootNode.childNodes;
	var catLevel1Node = null;
	var id;
        //alert("2.02");
	for(var i=0;i<catLevel1Nodes.length;i++)
	{
		catLevel1Node = catLevel1Nodes.item(i);
		id = catLevel1Node.attributes.item(1).value;
		if(id == level1CatId)
		{
			var catLevel2Nodes = catLevel1Node.childNodes;
                        var catLevel2Node;
			var catLevel2NewOption = null;
			while(cat2IdSelect.options.length > 1)
			{
				var optionChild = cat2IdSelect.children(1);
				cat2IdSelect.removeChild(optionChild);
			}
			for(var j=0;j<catLevel2Nodes.length;j++)
                        {
                                catLevel2Node = catLevel2Nodes(j);
                                catLevel2NewOption = document.createElement("OPTION");
                                catLevel2NewOption.value = catLevel2Node.attributes.item(1).value;
                                catLevel2NewOption.text = catLevel2Node.attributes.item(2).value;
                                cat2IdSelect.add(catLevel2NewOption);
                        }
                }		
        }
        //alert("2.1");
        defaultSelect(cat2IdSelect,selectedCatId);
        //alert("2.2");
        initSelect3(pForm);
        //alert("2.3");
    }
    
    function initSelect3(pForm)
    {
        var selectedCatId = pForm.catId.value;
    	var level1CatId = pForm.cat1Id.options[pForm.cat1Id.selectedIndex].value
    	var level2CatId = pForm.cat2Id.options[pForm.cat2Id.selectedIndex].value
       	var cat3IdSelect = pForm.cat3Id;
    	var catRootNode = catsData.XMLDocument.selectSingleNode("cats");
    	var catLevel1Nodes = catRootNode.childNodes;
	var catLevel1Node = null;
	var id1;
	var id2;
	for(var i=0;i<catLevel1Nodes.length;i++)
	{
		catLevel1Node = catLevel1Nodes.item(i);
		id1 = catLevel1Node.attributes.item(1).value;
		if(id1 == level1CatId)
		{
			var catLevel2Nodes = catLevel1Node.childNodes;
                   	var catLevel2Node;
                	for(var j=0;j<catLevel2Nodes.length;j++)//level2
                        {
                            catLevel2Node = catLevel2Nodes(j);
                            id2 = catLevel2Node.attributes.item(1).value;
                            if(id2 == level2CatId)
                            {
                		var catLevel3Nodes = catLevel2Node.childNodes;
                		var catLevel3NewOption = null;
                		var catLevel3Node;
				while(cat3IdSelect.options.length > 1)
                                {
					var optionChild = cat3IdSelect.children(1);
					cat3IdSelect.removeChild(optionChild);
				}
                		for(var k=0;k<catLevel3Nodes.length;k++)//level 3
                		{
                                    catLevel3Node = catLevel3Nodes(k);
                                    catLevel3NewOption = document.createElement("OPTION");
		                    catLevel3NewOption.value = catLevel3Node.attributes.item(1).value;
		                    catLevel3NewOption.text = catLevel3Node.attributes.item(2).value;
		                    cat3IdSelect.add(catLevel3NewOption);
                		}
                            }
                       }//end for
                   }//end if
	}//end for
        defaultSelect(cat3IdSelect,selectedCatId);
    }
    /*
    *get the selected value when user clicks submit
    */
    function getSelectedCatIdValue(pForm)
    {
        var cat1IdValue = pForm.cat1Id.options[pForm.cat1Id.selectedIndex].value;
        var cat1IdText = pForm.cat1Id.options[pForm.cat1Id.selectedIndex].text;
        var cat2IdValue = pForm.cat2Id.options[pForm.cat2Id.selectedIndex].value;
        var cat2IdText = pForm.cat2Id.options[pForm.cat2Id.selectedIndex].text;
        var cat3IdValue = pForm.cat3Id.options[pForm.cat3Id.selectedIndex].value;
        var cat3IdText = pForm.cat3Id.options[pForm.cat3Id.selectedIndex].text;
        var selectedCatIdText;
        var selectedCatIdValue;
        if(cat3IdValue.length > 0)
        {
            selectedCatIdValue = cat3IdValue;
            selectedCatIdText = cat3IdText;
        }
        else
        {
            if(cat2IdValue.length > 0)
            {
                selectedCatIdValue = cat2IdValue;
                selectedCatIdText = cat2IdText;
            }
            else
            {
                if(cat1IdValue.length > 0)
                {
                    selectedCatIdValue = cat1IdValue;
                    selectedCatIdText = cat1IdText;
                }
                else
                {
                    selectedCatIdValue = "";
                    selectedCatIdText = "";
                }
            }
        }
        if(selectedCatIdValue == null)selectedCatIdValue = "";
        return selectedCatIdValue;
    }
    
    
    